//
//  NSString+Extension.m
//  zdOrderPlatform
//
//  Created by dfwx on 2020/8/26.
//  Copyright © 2020 zhongdaodianzi. All rights reserved.
//

#import "NSString+Extension.h"
#import <CommonCrypto/CommonDigest.h>



@implementation NSString (Extension)

/**
*  普通字符串转化成十六进制字符串
**/
- (NSString *)ConvertOrdinaryStringToHexadecimal {
    
    NSData *myD = [self dataUsingEncoding:NSUTF8StringEncoding];
    Byte *bytes = (Byte *)[myD bytes];
    NSString *hexStr=@"";
    for(int i=0;i<[myD length];i++) {
        NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff]; //16进制数
        if([newHexStr length]==1) {
            hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
        } else {
            hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
        }
    }
    return hexStr;
}
/**
*  十六进制字符串转化成普通字符串
**/
- (NSString *)ConvertHexadecimalToOrdinaryString {
    
    char *myBuffer = (char *)malloc((int)[self length] / 2 + 1);
    bzero(myBuffer, [self length] / 2 + 1);
    for (int i = 0; i < [self length] - 1; i += 2) {
        unsigned int anInt;
        NSString * hexCharStr = [self substringWithRange:NSMakeRange(i, 2)];
        NSScanner * scanner = [[NSScanner alloc] initWithString:hexCharStr];
        [scanner scanHexInt:&anInt];
        myBuffer[i / 2] = (char)anInt;
    }
    NSString *unicodeString = [NSString stringWithCString:myBuffer encoding:4];
    
    return unicodeString;
}
/**
 *  十六进制字符转化成10进制
 **/
- (NSString *)ConvertedHexAdecimalStringToDecimalString {
    
    NSString * cardId2 = [NSString stringWithFormat:@"%ld",strtoul([self UTF8String],0,16)];
//    NSLog(@"%@:%@",[self class],NSStringFromSelector(_cmd));
//    NSLog(@"%@",cardId2);
    
    return cardId2;
}
/**
 *  十进制字符转化成16进制
 **/
- (NSString *)ConvertedDecimalStringToHexdecimalString {
    
//    NSLog(@"%@:%@",[self class],NSStringFromSelector(_cmd));
    if ([NSString stringWithFormat:@"%lx",self.integerValue].length%2 != 0) {
//        NSLog(@"%@",[NSString stringWithFormat:@"0%lx",self.integerValue]);
        return [NSString stringWithFormat:@"0%lx",self.integerValue];
    }
//    NSLog(@"%@",[NSString stringWithFormat:@"%lx",self.integerValue]);
    return [NSString stringWithFormat:@"%lx",self.integerValue];
}

/**
 *  16进制转data
 */
- (NSData *)convertHexStrToData {
    
    if (!self || [self length] == 0) {
        return nil;
    }
    NSMutableData *hexData = [[NSMutableData alloc] initWithCapacity:20];
    NSRange range;
    if ([self length] % 2 == 0) {
        range = NSMakeRange(0, 2);
    } else {
        range = NSMakeRange(0, 1);
    }
    for (NSInteger i = range.location; i < [self length]; i += 2) {
        unsigned int anInt;
        NSString *hexCharStr = [self substringWithRange:range];
        NSScanner *scanner = [[NSScanner alloc] initWithString:hexCharStr];
        
        [scanner scanHexInt:&anInt];
        NSData *entity = [[NSData alloc] initWithBytes:&anInt length:1];
        [hexData appendData:entity];
        
        range.location += range.length;
        range.length = 2;
    }
//    NSLog(@"%@:%@\nstr:%@\ndata:%@",[self class],NSStringFromSelector(_cmd),self,hexData);

    return hexData;
}


+ (NSString *)replaceUnicode:(NSString *)unicodeStr {
    
    NSString *tempStr1 = [unicodeStr stringByReplacingOccurrencesOfString:@"\\u" withString:@"\\U"];
    NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
    NSString *tempStr3 = [[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
    NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
    NSString *str = [NSPropertyListSerialization propertyListWithData:tempData
                                                              options:0
                                                               format:NULL
                                                                error:NULL];
    
    return [str stringByReplacingOccurrencesOfString:@"\\r\\n" withString:@"\n"];
}

//+ (NSString*)deviceVersion {
//    // 需要#import "sys/utsname.h"
//    struct utsname systemInfo;
//    uname(&systemInfo);
//    NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
//    
//    if ([deviceString isEqualToString:@"iPhone4,1"]) return @"iPhone 4S (A1387/A1431)";
//    if ([deviceString isEqualToString:@"iPhone5,1"]) return @"iPhone 5 (A1428)";
//    if ([deviceString isEqualToString:@"iPhone5,2"]) return @"iPhone 5 (A1429/A1442)";
//    if ([deviceString isEqualToString:@"iPhone5,3"]) return @"iPhone 5c (A1456/A1532)";
//    if ([deviceString isEqualToString:@"iPhone5,4"]) return @"iPhone 5c (A1507/A1516/A1526/A1529)";
//    if ([deviceString isEqualToString:@"iPhone6,1"]) return @"iPhone 5s (A1453/A1533)";
//    if ([deviceString isEqualToString:@"iPhone6,2"]) return @"iPhone 5s (A1457/A1518/A1528/A1530)";
//    if ([deviceString isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus (A1522/A1524)";
//    if ([deviceString isEqualToString:@"iPhone7,2"]) return @"iPhone 6 (A1549/A1586)";
//    if ([deviceString isEqualToString:@"iPhone8,1"])   return @"iPhone 6S";
//    if ([deviceString isEqualToString:@"iPhone8,2"])   return @"iPhone 6S Plus";
//    
//    if([deviceString  isEqualToString:@"iPhone8,4"])  return@"iPhone SE";
//    
//    if([deviceString  isEqualToString:@"iPhone9,1"])  return@"iPhone 7";
//    
//    if([deviceString  isEqualToString:@"iPhone9,2"])  return@"iPhone 7 Plus";
//    
//    if([deviceString  isEqualToString:@"iPhone10,1"]) return@"iPhone 8";
//    
//    if([deviceString  isEqualToString:@"iPhone10,4"]) return@"iPhone 8";
//    
//    if([deviceString  isEqualToString:@"iPhone10,2"]) return@"iPhone 8 Plus";
//    
//    if([deviceString  isEqualToString:@"iPhone10,5"]) return@"iPhone 8 Plus";
//    
//    if([deviceString  isEqualToString:@"iPhone10,3"]) return@"iPhone X";
//    if([deviceString  isEqualToString:@"iPhone10,6"]) return@"iPhone X";
//    
//    if([deviceString  isEqualToString:@"iPhone11,8"]) return@"iPhone XR";
//    
//    if([deviceString  isEqualToString:@"iPhone11,2"]) return@"iPhone XS";
//    
//    if([deviceString  isEqualToString:@"iPhone11,4"]) return@"iPhone XS Max";
//    if([deviceString  isEqualToString:@"iPhone11,6"]) return@"iPhone XS Max";
//    
//    
//    
//    if ([deviceString isEqualToString:@"iPod1,1"])   return @"iPod Touch 1G (A1213)";
//    if ([deviceString isEqualToString:@"iPod2,1"])   return @"iPod Touch 2G (A1288)";
//    if ([deviceString isEqualToString:@"iPod3,1"])   return @"iPod Touch 3G (A1318)";
//    if ([deviceString isEqualToString:@"iPod4,1"])   return @"iPod Touch 4G (A1367)";
//    if ([deviceString isEqualToString:@"iPod5,1"])   return @"iPod Touch 5G (A1421/A1509)";
//    
//    if ([deviceString isEqualToString:@"iPad1,1"])   return @"iPad 1G (A1219/A1337)";
//    
//    if ([deviceString isEqualToString:@"iPad2,1"])   return @"iPad 2 (A1395)";
//    if ([deviceString isEqualToString:@"iPad2,2"])   return @"iPad 2 (A1396)";
//    if ([deviceString isEqualToString:@"iPad2,3"])   return @"iPad 2 (A1397)";
//    if ([deviceString isEqualToString:@"iPad2,4"])   return @"iPad 2 (A1395+New Chip)";
//    if ([deviceString isEqualToString:@"iPad2,5"])   return @"iPad Mini 1G (A1432)";
//    if ([deviceString isEqualToString:@"iPad2,6"])   return @"iPad Mini 1G (A1454)";
//    if ([deviceString isEqualToString:@"iPad2,7"])   return @"iPad Mini 1G (A1455)";
//    
//    if ([deviceString isEqualToString:@"iPad3,1"])   return @"iPad 3 (A1416)";
//    if ([deviceString isEqualToString:@"iPad3,2"])   return @"iPad 3 (A1403)";
//    if ([deviceString isEqualToString:@"iPad3,3"])   return @"iPad 3 (A1430)";
//    if ([deviceString isEqualToString:@"iPad3,4"])   return @"iPad 4 (A1458)";
//    if ([deviceString isEqualToString:@"iPad3,5"])   return @"iPad 4 (A1459)";
//    if ([deviceString isEqualToString:@"iPad3,6"])   return @"iPad 4 (A1460)";
//    
//    if ([deviceString isEqualToString:@"iPad4,1"])   return @"iPad Air (A1474)";
//    if ([deviceString isEqualToString:@"iPad4,2"])   return @"iPad Air (A1475)";
//    if ([deviceString isEqualToString:@"iPad4,3"])   return @"iPad Air (A1476)";
//    if ([deviceString isEqualToString:@"iPad4,4"])   return @"iPad Mini 2G (A1489)";
//    if ([deviceString isEqualToString:@"iPad4,5"])   return @"iPad Mini 2G (A1490)";
//    if ([deviceString isEqualToString:@"iPad4,6"])   return @"iPad Mini 2G (A1491)";
//    
//    if ([deviceString isEqualToString:@"i386"])      return @"iPhone Simulator";
//    if ([deviceString isEqualToString:@"x86_64"])    return @"iPhone Simulator";
//    
//    NSLog(@"手机型号: %@", deviceString);
//    return deviceString;
//}

- (NSString *)md5 {
    
    const char *cStr = [self UTF8String];
    unsigned char result[16];
    CC_MD5( cStr, (unsigned int) strlen(cStr), result);
    return [NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
}

- (NSString*) sha1 {
    
    const char *cstr = [self cStringUsingEncoding:NSUTF8StringEncoding];
    NSData *data = [NSData dataWithBytes:cstr length:self.length];
    
    uint8_t digest[CC_SHA1_DIGEST_LENGTH];
    
    CC_SHA1(data.bytes, data.length, digest);
    
    NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
    
    for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    
    return output;
}

+ (NSMutableString *)versionNumberFormat:(NSString *)string {
    
    NSMutableString *result = [NSMutableString string];
    NSArray *array = [string componentsSeparatedByString:@"."];
    for (NSString *str in array) {
        [result appendFormat:@"%.3d",[str intValue]];
    }
    return result;
}

- (CGFloat)getStringLineSpacing:(CGFloat)lineSpacing font:(UIFont*)font width:(CGFloat)width {
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineSpacing = lineSpacing;
    NSDictionary *dic = @{ NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle };
    CGSize size = [self boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options: NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil].size;
    return  ceilf(size.height);
}

- (NSString *)subStringWithRangeForNil:(NSRange)range {
    
    if (range.location +range.length > self.length) {
        return @"";
    }
    return [self substringWithRange:range];
}

@end
